<?php
/*
 * 业务层
 */
namespace app\common\business;
use app\common\lib\classArr;
use app\common\lib\Num;
use app\common\lib\sms\AliSms;
use think\facade\Db;


class Sms{
    public static function sendCode($phone,$length,$type='ali'){
        //需要生成验证码
        $code = Num::getCode($length);
//        $sms = AliSms::sendCode($phone,$code);
        //工厂模式
//        $type = ucfirst($type);
//        $class = "app\common\lib\sms\\".$type."Sms";
//        $sms = $class::sendCode($phone,$code);
        //工厂模式2
        $classStat = classArr::smsClassStat();
        $classObj = classArr::initClass($type,$classStat);
        $sms = $classObj::sendCode($phone,$code);
        $data = [
            'phone'=>$phone,
            'code'=>$code,
            'add_time' =>date('Y-m-d H:i:s',time()),
            'type'=>1
        ];
        $sms1 = Db::table('shop_sms_code')->save($data);
        if($sms){
            //短信验证码记录到redis里面，并设置失效时间
            //1:php安装redis扩展
            //2:启动redis服务
            cache(config('redis.code_pre').$phone,$code,config('redis.code_expire'));
            return true;
        }else{
            return false;
        }

    }
}
